#!/bin/bash
set -e

bundledir="$1"
destdir="$bundledir/Contents/bin"
shift 1
qtdir="$1"
shift 1

mkdir -p "$destdir/../PlugIns"
mkdir -p "$destdir/../PlugIns/imageformats"
mkdir -p "$destdir/../PlugIns/iconengines"
mkdir -p "$destdir/../PlugIns/platforms"
mkdir -p "$destdir/../Frameworks"
mkdir -p "$destdir/../Resources"

{
	echo '[Paths]'
	echo 'Plugins=../PlugIns'
} > "$destdir/qt.conf"

filetofix=`mktemp /tmp/fnt-create-dmg-XXXXXX`
filetofixnew=`mktemp /tmp/fnt-create-dmg-XXXXXX`
otooloutput=`mktemp /tmp/fnt-create-dmg-XXXXXX`
find "$destdir" -type f > "$filetofix"
find "$destdir/../lib/fnt-plugins" -type f >> "$filetofix"

for p in `cd $qtdir/plugins/imageformats; ls`; do
	cp -R "$qtdir/plugins/imageformats/$p" "$destdir/../PlugIns/imageformats/$p"
	chmod -R u+w "$destdir/../PlugIns/imageformats/$p"
	install_name_tool -id "@executable_path/../PlugIns/imageformags/$p" "$destdir/../PlugIns/imageformats/$p"
	echo "$destdir/../PlugIns/imageformats/$p" >> "$filetofix"
done
for p in `cd $qtdir/plugins/iconengines; ls`; do
	cp -R "$qtdir/plugins/iconengines/$p" "$destdir/../PlugIns/iconengines/$p"
	chmod -R u+w "$destdir/../PlugIns/iconengines/$p"
	install_name_tool -id "@executable_path/../PlugIns/iconengines/$p" "$destdir/../PlugIns/iconengines/$p"
	echo "$destdir/../PlugIns/iconengines/$p" >> "$filetofix"
done
cp -R "$qtdir/plugins/platforms/libqcocoa.dylib" "$destdir/../PlugIns/platforms/libqcocoa.dylib"
chmod -R u+w "$destdir/../PlugIns/platforms/libqcocoa.dylib"
install_name_tool -id "@executable_path/../PlugIns/platforms/libqcocoa.dylib" "$destdir/../PlugIns/platforms/libqcocoa.dylib"
echo "$destdir/../PlugIns/platforms/libqcocoa.dylib" >> "$filetofix"

while [ -s "$filetofix" ]; do
	> "$filetofixnew"
	while read f; do
		otool -L -X "$f" > "$otooloutput"
		while read l; do
			l=`echo "$l" | sed 's,^ *,,'`
			l=`echo "$l" | sed 's, *(compatibility version.*$,,'`
			case "$l" in
				@executable_path/*)
					:
					;;
				@rpath/*)
					:
					;;
				/usr/lib/*)
					:
					;;
				/System/*)
					:
					;;
				*.dylib)
					destfile="../Frameworks/$(basename $l)"
					if [ ! -e "$destdir/$destfile" ]; then
						cp -R "$l" "$destdir/../Frameworks"
						chmod -R u+w "$destdir/$destfile"
						install_name_tool -id "@executable_path/$destfile" "$destdir/$destfile"
						echo "$destdir/$destfile" >> "$filetofixnew"
					fi
					install_name_tool -change "$l" "@executable_path/$destfile" "$f"
					;;
				*.framework/*)
					a=${l%.framework/*}
					destfile="../Frameworks/$(basename $a).framework"
					b=${l#*.framework/}
					if [ ! -e "$destdir/$destfile" ]; then
						{ cd "$a.framework"; tar -cpf - --exclude 'Headers' --exclude '*.prl' *; } | { mkdir -p "$destdir/$destfile"; tar -xpf - -C "$destdir/$destfile"; }
						chmod -R u+w "$destdir/$destfile"
						install_name_tool -id "@executable_path/$destfile/$b" "$destdir/$destfile/$b"
						echo "$destdir/$destfile/$b" >> "$filetofixnew"
					fi
					install_name_tool -change "$l" "@executable_path/$destfile/$b" "$f"
					;;
				*)
					echo "How to handle this?" >&2
					echo "'$l'" >&2
					exit -1
					;;
			esac
		done < "$otooloutput"
	done < "$filetofix"
	mv "$filetofixnew" "$filetofix"
done
rm -f "$filetofix"
rm -f "$filetofixnew"
rm -f "$otooloutput"
readme="$1"
shift
dmgname="$1"
shift
hdiutil create "$dmgname.dmg" -srcfolder "$readme" -srcfolder "$bundledir" -format "UDZO" -volname "$dmgname"
exit 0
